package tpalgo3.vista;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import tpalgo3.juego.Juego;
import tpalgo3.modelo.torres.TorreBlanca;
import tpalgo3.modelo.torres.TorreDorada;
import tpalgo3.modelo.torres.TorrePlateada;
import tpalgo3.vista.util.VistaUtil;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class SeleccionTorre extends JFrame implements ActionListener{
	private static final long serialVersionUID = 9071148671945813113L;
	
	private static final String BLANCA = "Blanca";
	private static final String PLATEADA = "Plateada";
	private static final String DORADA = "Dorada";
	
	private ButtonGroup group;
	
	private Juego juego;
	private int equis;
	private int igriega;
	private Tablero parent;
	
	public SeleccionTorre(Juego juego, Tablero parent, int equis, int igriega) {
		super("Poner torre");
		
		this.juego = juego;
		this.equis = equis;
		this.igriega = igriega;
		this.parent = parent;
		
		this.setSize(235, 178);
		this.setLocationRelativeTo(null);
		this.setIconImage(VistaUtil.createImageIcon("img/bala.png").getImage());

		FormLayout layout = new FormLayout(
				new ColumnSpec[]{
						new ColumnSpec("235px")
				},
				new RowSpec[]{
						new RowSpec("35px"),
						new RowSpec("68px"),
						new RowSpec("35px")
				});
		JPanel contenedor = new JPanel(layout);
		JPanel barraSuperior = new JPanel();
		barraSuperior.setBackground(Color.ORANGE);
		JLabel leyendaBarraSuperior = new JLabel("Seleccione torre...");
		leyendaBarraSuperior.setFont(new Font("Arial", Font.BOLD, 14));
		barraSuperior.add(leyendaBarraSuperior);
		contenedor.add(barraSuperior, new CellConstraints(1, 1));
		
		JRadioButton blancaButton = new JRadioButton(BLANCA + " $" + TorreBlanca.COSTO);
        blancaButton.setMnemonic(KeyEvent.VK_B);
        blancaButton.setActionCommand(BLANCA);
        blancaButton.setSelected(true);

        JRadioButton plateadaButton = new JRadioButton(PLATEADA + " $" + TorrePlateada.COSTO);
        plateadaButton.setMnemonic(KeyEvent.VK_P);
        plateadaButton.setActionCommand(PLATEADA);
        
        JRadioButton doradaButton = new JRadioButton(DORADA + " $" + TorreDorada.COSTO);
        doradaButton.setMnemonic(KeyEvent.VK_D);
        doradaButton.setActionCommand(DORADA);
        
        group = new ButtonGroup();
        group.add(blancaButton);
        group.add(plateadaButton);
        group.add(doradaButton);
        
        JPanel radioPanel = new JPanel(new GridLayout(0, 1));
        radioPanel.add(blancaButton);
        radioPanel.add(plateadaButton);
        radioPanel.add(doradaButton);
        
        contenedor.add(radioPanel, new CellConstraints(1, 2));
                
        JButton aceptar = new JButton("Aceptar");
        aceptar.setMnemonic(KeyEvent.VK_A);
        aceptar.addActionListener(this);
       
        JButton cancelar = new JButton("Cancelar");
        cancelar.setMnemonic(KeyEvent.VK_C);
        cancelar.addActionListener(new ActionListener() {
        	@Override
        	public void actionPerformed(ActionEvent e) {
				SeleccionTorre.this.dispose();				
			}
        });
        
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.add(aceptar);
        buttonsPanel.add(cancelar);
        
        contenedor.add(buttonsPanel, new CellConstraints(1, 3));
        
        this.getContentPane().add(contenedor);
        this.setResizable(false);
        this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		CellConstraints cc = new CellConstraints();
		boolean pudoPoner;
		String torre = group.getSelection().getActionCommand();		
		if (group.getSelection().getActionCommand().equals(BLANCA)) {
			pudoPoner = juego.ponerTorreBlanca(new Point(this.equis, this.igriega));
			if (pudoPoner) {
				this.parent.add(TableroSquare.armaImagen("img/torreblanca.jpg", Color.BLACK, this.equis, this.igriega), cc.xy(this.equis + 1, this.igriega + 1));
			}
		} 
		else 
			if (group.getSelection().getActionCommand().equals(PLATEADA)) {
				pudoPoner = juego.ponerTorrePlateada(new Point(this.equis, this.igriega));
				if (pudoPoner) {
					this.parent.add(TableroSquare.armaImagen("img/torreplateada.jpg", Color.BLACK, this.equis, this.igriega), cc.xy(this.equis + 1, this.igriega + 1));
				}
			}
			else {
				pudoPoner = juego.ponerTorreDorada(new Point(this.equis, this.igriega));
				if (pudoPoner) {
					this.parent.add(TableroSquare.armaImagen("img/torredorada.jpg", Color.BLACK, this.equis, this.igriega), cc.xy(this.equis + 1, this.igriega + 1));
				}
			}
		if (!pudoPoner) {
			JOptionPane.showMessageDialog(this,
					"No se pudo poner torre " + torre + ". Revise si dispone de dinero o hay otra torre alli.",
					"Error",
					JOptionPane.ERROR_MESSAGE);
		}
		SeleccionTorre.this.dispose();
	}
}